import 'dart:convert';
import 'dart:ffi';
import 'dart:ui';
import 'dart:convert' as convert;
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'cartoon_page_mod/allpage.dart';
import 'cartoon_page_mod/cartoon_page_value_setting.dart';

class cartoon_page_value extends StatefulWidget {
  cartoon_page_value({Key key, this.cartoon_id, this.page, this.allNum})
      : super(key: key);
  // ignore: non_constant_identifier_names
  int cartoon_id;
  int page;
  int allNum;
  @override
  State<cartoon_page_value> createState() => _cartoon_page_valueState();
}

class _cartoon_page_valueState extends State<cartoon_page_value>
    with SingleTickerProviderStateMixin {
  PageController _PageController = new PageController();

  AnimationController _controller;
  Animation<Offset> _top_animation;

  AnimationController _bot_controller;
  Animation<Offset> _bot_animation;

  int _page_num = 0;
  bool _menu_state = true;
  // 间谍过家家 27话资源

  List<String> page_url0;
  String page_name = 'MISSION 27';
  int collect = 0;
  int pageAllNUm;
  double JdtDou = 0;
  // ignore: non_constant_identifier_names
  // List<String> page_url0=[];
  // 获取屏幕亮度:
  Future _future;
  @override
  void initState() {
    // double brightness = await Screen.brightness;
    // 设置屏幕亮度 0.5
// Screen.setBrightness(0.5);
    // TODO: implement initState
    _future = _getinformation();
    _controller = AnimationController(
        duration: const Duration(milliseconds: 300), vsync: this);
    _top_animation =
        Tween(begin: Offset(0, 0), end: Offset(0, 1)).animate(_controller);

    _bot_animation =
        Tween(begin: Offset(0, 0), end: Offset(0, -1)).animate(_controller);
    _controller.forward();
    // _getinformation();
    super.initState();
  }

  _getinformation() async {
    print('异步调用');
    _page_num = 0;
    Response response;
    var dio = Dio();
    response = await dio.get(
        'http://119.91.231.198:3033/getCarPage?cartoon_id=1&page=' +
            widget.page.toString());
    page_url0 = response.data['url_list'].cast<String>();
    pageAllNUm = page_url0.length;
    var res = convert.jsonDecode(response.toString());
    page_name = res['resultName'][0]['cartoon_page_part_name'];
    JdtDou = pageAllNUm.toDouble();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    _controller.dispose();
    super.dispose();
  }
  // int page

  @override
  Widget build(BuildContext context) {
    print(widget.allNum);
    print('测试');
    return MaterialApp(
      home: Scaffold(
        body: FutureBuilder(
          future: _future,
          builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
            if (snapshot.connectionState == ConnectionState.done) {
              return Stack(
                children: [
                  Container(
                    width: MediaQuery.of(context).size.height,
                    height: MediaQuery.of(context).size.height,
                    constraints: BoxConstraints(
                        maxWidth: MediaQuery.of(context).size.width),
                    child: Stack(overflow: Overflow.clip, children: [
                      PageView(
                          physics: _menu_state
                              ? NeverScrollableScrollPhysics()
                              : null,
                          onPageChanged: (value) {
                            setState(() {
                              _page_num = value;
                            });
                          },
                          controller: _PageController,
                          children: page_url0.map((e) {
                            return Container(
                              width: MediaQuery.of(context).size.height,
                              height: MediaQuery.of(context).size.height,
                              decoration: new BoxDecoration(
                                color: Colors.black,
                                image: DecorationImage(
                                  image: NetworkImage(e),
                                  //这里是从assets静态文件中获取的，也可以new NetworkImage(）从网络上获取
                                ),
                              ),
                              child: Row(
                                children: [
                                  InkWell(
                                    onTap: () {
                                      print('上一页');
                                      setState(() {
                                        _menu_state
                                            // ignore: unnecessary_statements
                                            ? {
                                                _menu_state = false,
                                                _controller.reverse()
                                              }
                                            : (_page_num != 0)
                                                ? _PageController.jumpToPage(
                                                    _page_num - 1)
                                                : null;
                                      });
                                    },
                                    child: Container(
                                      width:
                                          MediaQuery.of(context).size.width / 3,
                                      // color: Color.fromRGBO(50, 170, 255, 0.2),
                                    ),
                                  ),
                                  InkWell(
                                    onTap: () {
                                      print('呼出菜单');
                                      setState(() {
                                        _menu_state
                                            // ignore: unnecessary_statements
                                            ? {
                                                _menu_state = false,
                                                _controller.reverse()
                                              }
                                            // ignore: unnecessary_statements
                                            : {
                                                _menu_state = true,
                                                _controller.forward()
                                              };
                                      });
                                    },
                                    child: Container(
                                      width:
                                          MediaQuery.of(context).size.width / 3,
                                      // color: Color.fromRGBO(50, 170, 255, 0.2),
                                    ),
                                  ),
                                  InkWell(
                                    onTap: () {
                                      print('下一页');
                                      setState(() {
                                        _menu_state
                                            // ignore: unnecessary_statements
                                            ? {
                                                _menu_state = false,
                                                _controller.reverse()
                                              }
                                            // ignore: unnecessary_statements
                                            : {
                                                _PageController.jumpToPage(
                                                    _page_num + 1)
                                              };
                                      });
                                    },
                                    child: Container(
                                      width:
                                          MediaQuery.of(context).size.width / 3,
                                      // color: Color.fromRGBO(50, 170, 255, 0.2),
                                    ),
                                  ),
                                ],
                              ),
                            );
                          }).toList()),
                    ]),
                  ),
                  // 每个页面
                  // top
                  Transform.translate(
                    offset: Offset(0, -80),
                    child: SlideTransition(
                      position: _top_animation,
                      child: Container(
                        height: 80,
                        width: MediaQuery.of(context).size.width,
                        color: Color.fromRGBO(34, 34, 35, 1),
                        child: Row(
                          children: [
                            InkWell(
                              onTap: () {
                                Navigator.of(context).pop();
                              },
                              child: Container(
                                margin: EdgeInsets.only(
                                    left: 12, top: 10, right: 10),
                                child: Icon(
                                  Icons.arrow_back_ios_new,
                                  color: Colors.white,
                                ),
                              ),
                            ),
                            Container(
                              margin: EdgeInsets.only(top: 10),
                              child: Text(
                                widget.page.toString() + '话 ' + page_name,
                                style: TextStyle(
                                    color: Colors.white, fontSize: 20),
                              ),
                            )
                          ],
                        ),
                      ),
                    ),
                  ),
                  // top
                  // bottom
                  Positioned(
                      bottom: 0,
                      child: Transform.translate(
                        offset: Offset(0, 100),
                        child: SlideTransition(
                          position: _bot_animation,
                          child: Container(
                              height: 100,
                              width: MediaQuery.of(context).size.width,
                              color: Color.fromRGBO(34, 34, 35, 1),
                              child: Container(
                                // width: 100,
                                child: Column(
                                  children: [
                                    Container(
                                      // color: Colors.green,
                                      width: MediaQuery.of(context).size.width,
                                      // alignment: Alignment.center,
                                      child: Row(
                                        children: [
                                          Expanded(
                                            flex: 1,
                                            child: InkWell(
                                              onTap: () {
                                                print('上一话');
                                                if (widget.page != 1) {
                                                  widget.page--;
                                                  setState(() {
                                                    _future = _getinformation();
                                                  });
                                                }
                                              },
                                              child: Container(
                                                alignment: Alignment.center,
                                                child: Text(
                                                  '上一话',
                                                  style: TextStyle(
                                                      color: Colors.white),
                                                ),
                                              ),
                                            ),
                                          ),
                                          Expanded(
                                            flex: 2,
                                            child: Container(
                                              child: SliderTheme(
                                                data: SliderTheme.of(context)
                                                    .copyWith(
                                                        activeTickMarkColor:
                                                            Color.fromRGBO(
                                                                0, 0, 0, 0),
                                                        activeTrackColor: Color
                                                            .fromRGBO(49, 169,
                                                                254, 1),
                                                        inactiveTrackColor:
                                                            Colors.white30,
                                                        thumbColor:
                                                            Colors.white),
                                                child: Slider(
                                                  value: _page_num.toDouble(),
                                                  onChanged: (data) {
                                                    print('change:$data');
                                                    setState(() {
                                                      _page_num = data.toInt();
                                                    });
                                                  },
                                                  onChangeStart: (data) {
                                                    print('start:$data');
                                                  },
                                                  onChangeEnd: (data) {
                                                    print('end:$data');
                                                    _PageController.jumpToPage(
                                                        data.toInt());
                                                  },
                                                  min: 0.0,
                                                  max: JdtDou,
                                                  divisions: pageAllNUm,
                                                  label: (_page_num + 1)
                                                      .toString(),
                                                  // activeColor: Colors.blue[200],
                                                  // inactiveColor: Colors.grey,
                                                  semanticFormatterCallback:
                                                      (double newValue) {
                                                    return '${newValue.round()} dollars}';
                                                  },
                                                ),
                                              ),
                                            ),
                                          ),
                                          Expanded(
                                            flex: 1,
                                            child: InkWell(
                                              onTap: () {
                                                print('下一话');
                                                if (widget.page !=
                                                    widget.allNum) {
                                                  widget.page++;
                                                  setState(() {
                                                    _future = _getinformation();
                                                  });
                                                }
                                              },
                                              child: Container(
                                                alignment: Alignment.center,
                                                child: Text(
                                                  '下一话',
                                                  style: TextStyle(
                                                      color: Colors.white),
                                                ),
                                              ),
                                            ),
                                          ),
                                        ],
                                      ),
                                    ),
                                    Row(
                                      children: [
                                        Expanded(
                                          flex: 1,
                                          child: InkWell(
                                            onTap: () {
                                              setState(() {
                                                collect == 0
                                                    ? collect = 1
                                                    : collect = 0;
                                              });
                                            },
                                            child: Column(
                                              children: [
                                                Container(
                                                  margin: EdgeInsets.only(
                                                      top: 0, right: 4),
                                                  child: Image.asset(
                                                    'assets/images/common/cartoon/shoucang.png',
                                                    color: collect == 0
                                                        ? Colors.white
                                                        : Color.fromRGBO(
                                                            254, 157, 16, 1),
                                                  ),
                                                ),
                                                Container(
                                                  child: Text(
                                                    collect == 0 ? '追漫' : '已追',
                                                    style: TextStyle(
                                                        fontSize: 16,
                                                        color: Colors.white),
                                                  ),
                                                )
                                              ],
                                            ),
                                          ),
                                        ),
                                        Expanded(
                                            child: Column(
                                          children: [
                                            Icon(
                                              Icons.light_mode_rounded,
                                              color: Colors.white,
                                            ),
                                            Text(
                                              '亮度',
                                              style: TextStyle(
                                                  fontSize: 16,
                                                  color: Colors.white),
                                            ),
                                          ],
                                        )),
                                        // Expanded(
                                        //     child: InkWell(
                                        //   onTap: () {
                                        //     // showModalBottomSheet 点击出现底部菜单
                                        //     showModalBottomSheet<Void>(
                                        //         context: context,
                                        //         builder:
                                        //             (BuildContext context) {
                                        //           return cartoon_page_value_setting();
                                        //         });
                                        //   },
                                        //   child: Column(
                                        //     children: [
                                        //       Icon(
                                        //         Icons.settings,
                                        //         color: Colors.white,
                                        //       ),
                                        //       Text(
                                        //         '设置',
                                        //         style: TextStyle(
                                        //             fontSize: 16,
                                        //             color: Colors.white),
                                        //       ),
                                        //     ],
                                        //   ),
                                        // )),
                                        Expanded(
                                            child: InkWell(
                                          onTap: () {
                                            // showModalBottomSheet 点击出现底部菜单
                                            showModalBottomSheet<Void>(
                                                context: context,
                                                builder:
                                                    (BuildContext context) {
                                                  return allpage(
                                                    partnum: (value) {
                                                      widget.page = value;
                                                      setState(() {
                                                        _future =
                                                            _getinformation();
                                                      });
                                                    },
                                                  );
                                                });
                                          },
                                          child: Column(
                                            children: [
                                              Icon(
                                                Icons.library_books_rounded,
                                                color: Colors.white,
                                              ),
                                              Text(
                                                '章节',
                                                style: TextStyle(
                                                    fontSize: 16,
                                                    color: Colors.white),
                                              ),
                                            ],
                                          ),
                                        )),
                                      ],
                                    )
                                  ],
                                ),
                              )),
                        ),
                      )),
                  // bottom
                ],
              );
            } else {
              return Container();
            }
          },
        ),
      ),
    );
  }
}
